﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GameProtocol;
using GameProtocol.DTO;
using LOLServer.Biz;
using LOLServer.Biz.Account.Interface;
using LOLServer.Tools;
using NetFrame;
using NetFrame.Auto;

namespace LOLServer.Logic.Login
{
    /*登录流程
     * 1、快速登录(类似于注册)
     * (1)客户端发送随机标识码Token
     * (2)服务器回复客户端唯一标识码AccountId和交互令牌Token，并存储AccountId在以后的交互中使用AccountId和Token相加的MD5
     * (3)客户端存储AccountId，并在以后的交互中使用
     * 2、账号登录
     * (1)客户端在内存中查找存储的AccountId和Token，如果存在，则向服务器验证是否存在和Token是否正确，存在并且正确则登录成功
     * (2)客户端内存中没有AccountId，则提示使用快速登录
     */
    /// <summary>
    /// 登陆逻辑层
    /// </summary>
    public class LoginHandler : AbsHandlerBase, HandlerInterface
    {
        private IAccountBiz mAccountBiz = BizFactory.mAccountBiz;
        private string mPassToken = "";
        public void ClientClose(UserToken token, string error)
        {
            ExecutorPool.Instance.Executor(delegate()
            {
                mAccountBiz.DisConnect(token, error);
            });
        }

        void login(UserToken token, AccountInfoDTO value)
        {
            ExecutorPool.Instance.Executor(delegate ()
            {
                int accountId = 0;
                int res = mAccountBiz.LoginAccount(token, value.AccountId,value.PassToken, ref accountId);
                switch (res)
                {
                    case 0:
                        SendMsg(token, GetGameType(), LoginProtocol.LOGIN_SRES, accountId);
                        break;
                    case 1:
                        SendMsg(token, GetGameType(), LoginProtocol.LOGIN_SRES, 1);
                        break;
                }
            });
        }
        void reg(UserToken token, AccountInfoDTO value)
        {
            ExecutorPool.Instance.Executor(delegate ()
            {
                int accountId = 0;
                int res = mAccountBiz.CreateAccount(token, value.PassToken, ref accountId);
                switch (res)
                {
                    case 0:
                        SendMsg(token, GetGameType(), LoginProtocol.REG_SRES, accountId);
                        break;
                    case 1:
                        SendMsg(token, GetGameType(), LoginProtocol.REG_SRES, 1);
                        break;
                    case 2:
                        SendMsg(token, GetGameType(), LoginProtocol.REG_SRES, 2); 
                        break;
                }
              
            });
        }
        /// <summary>
        /// 握手
        /// </summary>
        /// <param name="token"></param>
        /// <param name="value"></param>
        void HandShake(UserToken token, string value)
        {
            ExecutorPool.Instance.Executor(delegate ()
            {
                SendMsg(token, GetGameType(), LoginProtocol.HAND_SHAKE_SRES, mAccountBiz.HandShake(token, value));
            });
        }

        public void ClientConnect(NetFrame.UserToken token)
        {

        }
        public void MessageReceive(UserToken token, SocketModel message)
        {
            switch (message.Command)
            {
                case LoginProtocol.LOGIN_CREQ:
                    login(token, message.GetMessage<AccountInfoDTO>());
                    break;
                case LoginProtocol.REG_CREQ:
                    reg(token, message.GetMessage<AccountInfoDTO>());
                    break;
                case LoginProtocol.HAND_SHAKE_CREQ:
                    HandShake(token, message.GetMessage<string>());
                    break;
            }
        }

        public override byte GetGameType()
        {
            return Protocol.TYPE_LOGIN;
        }
    }
}
